package cn.senjoeson.triper.fragment;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import cn.senjoeson.mylibrary.utils.AppUtils;
import cn.senjoeson.triper.R;
import cn.senjoeson.triper.adapter.FavoriteListAdapter;
import cn.senjoeson.triper.base.BaseFragment;
import cn.senjoeson.triper.base.GreenDaoManager;
import cn.senjoeson.triper.bean.FavoriteBean;
import cn.senjoeson.triper.dao.FavoriteBeanDao;

/**
 * Created by senjoeson on 2017/12/29.
 */

public class FavoriteFragment extends BaseFragment {


    @BindView(R.id.empty)
    RelativeLayout mEmpty;
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerview;
    @BindView(R.id.left_arrow)
    ImageView mLeftArrow;
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.right_arrow)
    ImageView mRightArrow;
    @BindView(R.id.toolbarlayout)
    LinearLayout mToolbarlayout;
    public FavoriteListAdapter mFavoriteListAdapter;



    @Override
    protected void initView() {
        mTitle.setText("Favorites");
    }

    @Override
    protected int setContentResId() {
        return R.layout.frament_favorite;
    }

    @Override
    protected void initSetData() {
        //   mPlaceBeans = new ArrayList<>();
        FavoriteBeanDao favoriteBeanDao = GreenDaoManager.getInstance().getNewSession().getFavoriteBeanDao();
        List<FavoriteBean> list = favoriteBeanDao.queryBuilder().build().list();
        if (list != null && list.size() > 0) {
            mRecyclerview.setLayoutManager(new GridLayoutManager(AppUtils.getContext(), 2));
            mFavoriteListAdapter = new FavoriteListAdapter(getContext(), list);
            mRecyclerview.setAdapter(mFavoriteListAdapter);
            mEmpty.setVisibility(View.GONE);
        } else {
            mEmpty.setVisibility(View.VISIBLE);
        }


       /* OkHttpUtils.get().url("http://www.senjoeson.cn/trip.json").build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.showToast("访问错误");
            }

            @Override
            public void onResponse(String response, int id) {
                InterDataBean interDataBean = new Gson().fromJson(response, InterDataBean.class);
                final List<InterDataBean.ResultBean.ValueBean> values = interDataBean.getResult().getValue();

                for (int i = 0; i < values.size(); i++) {
                    InterDataBean.ResultBean.ValueBean valueBean = values.get(i);
                    PlaceBean placeBean = new PlaceBean();
                    placeBean.setId(i);
                    placeBean.setPlace(valueBean.getName());
                    placeBean.setImageUrl(valueBean.getPicImg());
                    mPlaceBeans.add(placeBean);
                }




                mFavoriteListAdapter.setOnItemClickListener(new FavoriteListAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(View v, int position) {
                        InterDataBean.ResultBean.ValueBean valueBean = values.get(position);
                        PlaceDetailBean placeDetailBean = new PlaceDetailBean();
                        placeDetailBean.setImageUrl(valueBean.getPicImg());
                        placeDetailBean.setAddress(valueBean.getAddress());
                        placeDetailBean.setIntroduction(valueBean.getIntroduce());
                        placeDetailBean.setTel(valueBean.getTelephone());
                        placeDetailBean.setPlaceName(valueBean.getName());
                        placeDetailBean.setOpenTime("暂无信息");
                        placeDetailBean.setWebsite("暂无信息");
                        placeDetailBean.setAvgStayTime("暂无");
                        placeDetailBean.setPlaceId(position);
                        GreenDaoManager.getInstance().getNewSession().getPlaceDetailBeanDao().insertWithoutSettingPk(placeDetailBean);
                        Intent intent = new Intent(AppUtils.getContext(), DestinationActivity.class);
                        intent.putExtra("placeId", (long)position);
                        startActivity(intent);
                    }
                });
            }
        });*/


    }

    @Override
    protected void initListener() {
        mLeftArrow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getMainActivity().mViewPager.setCurrentItem(1);
            }
        });
    }


}
